package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.mapper.DeviceMapper;
import com.milotnt.pojo.DeviceEntity;
import com.milotnt.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, DeviceEntity> implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public List<DeviceEntity> findAll() {
        LambdaQueryWrapper<DeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        return deviceMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean deleteByDeviceId(Long deviceId) {
        return deviceMapper.deleteByDeviceId(deviceId);
    }

    @Override
    public Boolean insertDevice(DeviceEntity device) {
        Date date = new Date();
        device.setCreateTime(date);
        String deviceStatus = device.getDeviceStatus().getDescription();
        return deviceMapper.insertDevice(device, deviceStatus);
    }

    @Override
    public Boolean updateDeviceByDeviceId(DeviceEntity device) {
        Date date = new Date();
        device.setUpdateTime(date);
        String deviceStatus = device.getDeviceStatus().getDescription();
        return deviceMapper.updateDeviceByDeviceId(device, deviceStatus);
    }

    @Override
    public List<DeviceEntity> selectByDeviceId(Long deviceId) {
        return deviceMapper.selectByDeviceId(deviceId);
    }

    @Override
    public Integer selectTotalCount() {
        return deviceMapper.selectTotalCount();
    }
}
